package gang.com.mobelhelper.ui.activity;


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.widget.Button;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gang.com.mobelhelper.R;
import gang.com.mobelhelper.di.component.AppCompent;

/**
 * https://github.com/tbruyelle/RxPermissions RxPermissions框架的使用
 */
public class LoginActivity extends BaseActivity {


    private static final int READ_PHONE_STATE_CODE = 1000;
    @BindView(R.id.quanxian_check)
    Button quanxianCheck;

    @Override
    public int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void setupActivityComponent(AppCompent appCompent) {

    }

    @Override
    public void init() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.quanxian_check)
    public void onViewClicked() {
        //没有授权
        //第二步需要检查权限,如果不等于说明没有授权
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { //第三步我们申请授权
            //第三步授权
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, READ_PHONE_STATE_CODE);
        } else {
            //已经授权
            Toast.makeText(this, "授权成功", Toast.LENGTH_LONG).show();
        }
    }
    //处理授权回调的方法

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        //授权调用了
        if (requestCode == READ_PHONE_STATE_CODE) {
            //拿到那个结果
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "授权成功", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "用户拒绝授权", Toast.LENGTH_LONG).show();
            }
        }
    }
}
